﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UcAsp.Net.MSSQLDAL
{
    public class AttachementFileDAL : UcAsp.Net.IDAL.IAttachementFileDAL
    {
        public bool Delete(int fileid)
        {

            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                entities.Database.ExecuteSqlCommand("delete from AttachementFile where fileid ='" + fileid + "'");
                return true;
            }
        }
        public UcAsp.Net.Model.AttachementFile Insert(UcAsp.Net.Model.AttachementFile model)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {

                System.Data.Entity.Infrastructure.DbEntityEntry<UcAsp.Net.Model.AttachementFile> entry = entities.Entry<UcAsp.Net.Model.AttachementFile>(model);
                entry.State = System.Data.EntityState.Added;
                entities.AttachementFiles.Add(model);
                entities.SaveChanges();
                return model;
            }
        }
        public bool Update(UcAsp.Net.Model.AttachementFile model)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                System.Data.Entity.Infrastructure.DbEntityEntry<UcAsp.Net.Model.AttachementFile> entry = entities.Entry<UcAsp.Net.Model.AttachementFile>(model);
                entry.State = System.Data.EntityState.Modified;
                entities.SaveChanges();
                return true;
            }
        }
        public UcAsp.Net.Model.AttachementFile GetFileByMd5(string md5)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {

                return entities.AttachementFiles.FirstOrDefault(o => o.FileMd5 == md5);
            }
        }
        public UcAsp.Net.Model.AttachementFile GetFileByFileId(int fileid)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {

                return entities.AttachementFiles.FirstOrDefault(o => o.FileId == fileid);
            }
        }
        public List<UcAsp.Net.Model.AttachementFile> GetFileByRelatedId(int relatedid)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {

                return entities.AttachementFiles.Where(o => o.RelatedId == relatedid).ToList();
            }
        }
        public List<Model.AttachementFile> GetAttachementFilePage(int pageindex, int pagesize)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                return entities.AttachementFiles.OrderByDescending(o => o.FileId).Skip(pageindex * pagesize).Take(pagesize).ToList();
            }
        }
    }
}
